#组织架构中的添加部门页面
from page.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.alert import Alert

class addDepartment(BasePage):
    url = "/crm5/index.php?m=user&a=department"
    #添加部门定位器
    btn_add_department_locator=(By.ID,"add_department")
    # 部门名
    department_name_locator = (By.ID, "name")
    # 确认添加按钮
    btn_accept_locator = (By.XPATH,
                          '/html/body/div[8]/div[3]/div/button[1]/span')
    # 已添加的部门（12345test）定位器
    test_locator = (By.CSS_SELECTOR, '#browser > li:nth-child(7) > span')
    # 删除定位器
    delete_locator = (By.LINK_TEXT, '删除')
    #成功删除文本
    success_delete_loator=(By.CSS_SELECTOR,'body > div.container > div.alert.alert-success')
    #成功添加部门文本
    add_department_success_locator=(By.CSS_SELECTOR,'body > div.container > div.alert.alert-success')

    def click_add_department(self):
        """点击添加部门按钮"""
        self.driver.find_element(*self.btn_add_department_locator).click()  # 点击添加按钮
    def input_department_name(self, departmentname='12345test'):
        """输入部门名"""
        self.driver.find_element(*self.department_name_locator).send_keys(departmentname)
    def click_accept_department(self):
        """点击确定添加按钮"""
        self.driver.find_element(*self.btn_accept_locator).click()
    def click_delete_department(self):
        """点击删除，出现弹窗"""
        # 鼠标悬浮在12345test部门上面
        test_element = self.driver.find_element(*self.test_locator)  # 找到元素
        action = ActionChains(self.driver)  # 实例化操作鼠标事件的类
        action.move_to_element(test_element).perform()  # 悬停，提交
        # 点击删除，出现弹窗
        self.driver.find_element(*self.delete_locator).click()
    def delete_department(self,num=1):
        """删除"""
        # 切换弹窗进行操作
        alert = Alert(self.driver)  # 实例化
        if num == 1:
            alert.accept()#确定删除
        else:
            alert.dismiss()#取消删除
    def delete_success_text(self):
        """成功删除提示语"""
        return self.driver.find_element(*self.success_delete_loator).text

    def add_department_success_text(self):
        """部门添加成功提示语"""
        return self.driver.find_element(*self.add_department_success_locator).text

